package com.xiaoge.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.Menu;
import com.xiaoge.system.service.MenuService;
import com.xiaoge.system.vo.MenuVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/menu")
//允许跨域
@CrossOrigin
public class MenuController {

    private MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    /**
     * 通过用户id查询菜单列表
     *
     * @return
     */
    @ApiOperation(value = "通过用户id查询菜单列表")
    @GetMapping("/getMenusByAdminId")
    public Result getMenusByAdminId() {
        List<Menu> menus = menuService.getMenusByAdminId();
        return Result.ok().data("menus", menuService.menuTreeBuild(menus));
    }

    /**
     * 查询所有菜单列表
     *
     * @return
     */
    @ApiOperation(value = "查询所有菜单列表")
    @GetMapping("/getMenus")
    public Result getMenu() {
        List<Menu> menus = menuService.getAll();
        menus.sort(Menu.order());
        Set<String> urls = new HashSet<>();
        for (Menu menu : menus) {
            if (menu.getType().equals("1")&& !ObjectUtils.isEmpty(menu.getUrl())){
                urls.add(menu.getUrl());
            }
        }
        return Result.ok().data("menus", menus).data("urls",urls);
    }

    /**
     * 查询菜单列表
     *
     * @return
     */
    @ApiOperation(value = "查询菜单列表")
    @PostMapping("/getMenus")
    public Result getMenus(@RequestBody MenuVo menuVo) {
        try {
            QueryWrapper<Menu> queryWrapper = getWrapper(menuVo);
            List<Menu> menus = menuService.list(queryWrapper);
            return Result.ok().data("menus", menuService.permsTreeBuild(menus));
        }catch (Error error){
            return Result.error().message("没有查询到");
        }
    }

    /**
     * 查询下拉菜单列表
     *
     * @return
     */
    @ApiOperation(value = "查询下拉菜单列表")
    @GetMapping("/getMenuSelect")
    public Result getMenuSelect() {
        List<Menu> menus = menuService.getAll();
        return Result.ok().data("menus", menuService.menuTreeBuild(menus));
    }

    /**
     * 添加菜单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加菜单失败", operation = "添加菜单")
    @ApiOperation(value = "添加菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        menuService.add(menu);
        return Result.ok();
    }

    /**
     * 修改菜单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改菜单失败", operation = "修改菜单")
    @ApiOperation(value = "修改菜单")
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu) {
        menuService.updateMenu(menu);
        return Result.ok();
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除菜单失败", operation = "删除菜单")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delMenu")
    public Result delMenu(Long menuId) {
        menuService.delMenu(menuId);
        return Result.ok();
    }

    private QueryWrapper<Menu> getWrapper(MenuVo menuVo) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (menuVo != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(menuVo.getMenuName())) {
                queryWrapper.like("menu_name", menuVo.getMenuName());
            }
            if (!ObjectUtils.isEmpty(menuVo.getAvailable())) {
                queryWrapper.eq("available", menuVo.getAvailable());
            }
            if (!ObjectUtils.isEmpty(menuVo.getType())) {
                queryWrapper.eq("type", menuVo.getType());
            }
        }
        return queryWrapper;
    }
}
